// pages/search/result.js
var app = getApp();
const getGoodsListUrl = '/Goods/recommend';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isLoading:0,//商品是否已经加载完毕
    is_clear:'',//判断用户是否点击过清空按钮
    keywords:'',//搜索关键词
    is_disabled:0,//判断下一次是否不可以点击
    is_forward_ranking:0,//排序方式
    current:0,//当前排序的标准0无标准1价格2销量
    goodslist:[],
    page:1//商品列表的页码
  },
  // 清空搜索框的内容
  clearKeywords:function(){
    this.setData({
      goodslist: [],//商品列表
      page: 1,//商品列表的页码
      is_clear:1,
      keywords:''
    })
    this.searchEvent();
  },
  // 跳转商品详情页
  freeReceive: function (e) {
    var _this = this;
    var theGoodsId = e.currentTarget.dataset.goods_id;
    wx.navigateTo({
      url: '/pages/goodsDetail/goodsDetail?goodsId=' + theGoodsId,
    })
  },
  // 获取搜索的关键词
  getKeywords:function(e){
    this.setData({
      keywords:e.detail.value
    })
  },
  //开始搜索
  searchEvent:function(e){
    // console.log(e);
    if (this.data.is_clear==1){
      this.setData({
        is_clear:0
      })
      return false;
    }
    this.setData({
      goodslist:[],//商品列表
      page: 1//商品列表的页码
    })
    this.getGoodsList();
  },
  // 排序事件
  sortEvent:function(e){
    var _this = this;
    this.setData({
      goodslist: [],
      page: 1//商品列表的页码
    })
    var is_forward_ranking = _this.data.is_forward_ranking++;
    var is_disabled = _this.data.is_disabled;
    if (is_disabled==1){
      _this.setData({
        is_disabled:0,
        current:0,
        is_forward_ranking:0,
      })
    }else{
      if (is_forward_ranking == 1) {
        _this.setData({
          current: e.currentTarget.dataset.type,
          is_disabled: 1,
          is_forward_ranking: 0
        })
      } else {
        _this.setData({
          current: e.currentTarget.dataset.type,
          is_forward_ranking: 1
        })
      }
    }
    _this.getGoodsList();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  var _this = this;
    var configs = wx.getStorageSync('configs');
    _this.setData({
      filesSourceUri: configs.static_url_http
    })
  _this.loop();
  },
  // 判断是否获取到token的值了
  loop: function () {
    var _this = this;
    var theToken = wx.getStorageSync("token");
    if (!theToken) {
      setTimeout(function () { _this.loop() }, 100);
    } else {
      _this.setData({
        token: theToken
      });
    }
  },
  // 获取商品列表接口
  getGoodsList: function () {
    var _this = this;
    _this.setData({
      isLoading:1
    })
    var sort_type,
      current = _this.data.current,
      is_forward_ranking = _this.data.is_forward_ranking;
    if (current==0){
      sort_type=0;
    } else if (current == 1){
      if (is_forward_ranking==1){
        sort_type = 1;
      }else{
        sort_type = 2;
      }
    } else if (current == 2){
      if (is_forward_ranking == 1) {
        sort_type = 3;
      } else {
        sort_type = 4;
      }
    }
    var requestData = {
      token: _this.data.token,
      version: app.setConfig.version,
      type: app.setConfig.type,
      keywords: _this.data.keywords,
      sort_type: sort_type,
      search_type:1,
      page: _this.data.page
    }
    app.post(getGoodsListUrl, requestData, function (res) {
      if (res.code == 2000) {
        wx.hideNavigationBarLoading(); //完成停止加载
        wx.stopPullDownRefresh(); //停止下拉刷新
        var goodslist = _this.data.goodslist;
        goodslist = goodslist.concat(res.data.list);
        _this.setData({
          isLoading: 0,
          page: res.data.page,
          goodslist: goodslist
        })
      }else{
        wx.showToast({
          title: res.msg,
          icon: 'success',
          duration: 2000
        })
      }
    });
  }
})